#include "SpeedRunBuff.h"
#include "MainLayer.h"
#include "Config.h"
#include "TestGameScene.h"
#include "GameBackground.h"
#include "InputLayer.h"


SpeedRunBuff::SpeedRunBuff(void)
{
	_currentTime = 0;
	_listWave = NULL;
	_protectBoxBK = CCSizeZero;
}


SpeedRunBuff::~SpeedRunBuff(void)
{
	CC_SAFE_RELEASE(_listWave);
}

bool SpeedRunBuff::init()
{
	if (!BaseBuff::init())
		return false;

	CCSprite* sprite = CCSprite::createWithSpriteFrameName("undying_icon.png");
	sprite->setAnchorPoint(ccp(0.5f, 0.5f));
	sprite->setPosition(ccp(getBlackMask()->boundingBox().getMidX(), getBlackMask()->boundingBox().getMidY()));
	addChild(sprite, 0);

	getBlackMask()->setScaleY(1.0f);
	setWaves(CCArray::create());

	return true;
}

void SpeedRunBuff::onStart( StateBar* stateBar )
{
	PlayerPlane* plane = getPlane();
	plane->setIsProtected(true);

	// Effect for plane
	_ccBlendFunc f = {GL_SRC_ALPHA, GL_ONE};

	float scaleDelta = 0.3f;
	float scaleDelta2 = 0.5f;
	float scaleDelta1 = 0.1f;
	float timeScale = 0.2f;
	float timeScale2 = 0.2f;
	CCPoint planePoint = ccp(getPlane()->boundingBox().size.width/2, getPlane()->boundingBox().size.height/2) - ccp(0, 50);

	CCSprite* sprite1 = CCSprite::create("wsparticle_ring18.png");
	sprite1->setScale(4.0f);
	sprite1->setPosition(planePoint);
	sprite1->setBlendFunc(f);
	sprite1->runAction(CCRepeatForever::create(CCSequence::create(CCScaleTo::create(timeScale, sprite1->getScale() + scaleDelta), CCScaleTo::create(timeScale, sprite1->getScale() - scaleDelta), NULL)));
	plane->addChild(sprite1, 1, 20);

	CCSprite* sprite2 = CCSprite::create("wsparticle_ring11.png");
	sprite2->setScale(7.0f);
	sprite2->setOpacity(200);
	sprite2->setPosition(planePoint + ccp(0, -150));
	sprite2->setColor(ccc3(64, 198, 255));
	sprite2->setBlendFunc(f);
	sprite2->runAction(CCRepeatForever::create(CCSequence::create(CCScaleTo::create(timeScale2, sprite2->getScale() + scaleDelta), CCScaleTo::create(timeScale2, sprite2->getScale() - scaleDelta), NULL)));
	plane->addChild(sprite2, 1, 21);

	CCSprite* sprite3 = CCSprite::create("wsparticle_superglow01.png");
	sprite3->setScale(7.0f);
	sprite3->setPosition(planePoint + ccp(0, - 60));
	sprite3->setOpacity(150);
	sprite3->setColor(ccc3(64, 198, 255));
	sprite3->setBlendFunc(f);
	sprite3->runAction(CCRepeatForever::create(CCSequence::create(CCScaleTo::create(timeScale, sprite3->getScale() + scaleDelta2), CCScaleTo::create(timeScale, sprite3->getScale() - scaleDelta2), NULL)));
	plane->addChild(sprite3, 1, 22);

	CCSprite* sprite4 = CCSprite::create("wsparticle_superglow01.png");
	sprite4->setScale(15.0f);
	sprite4->setPosition(planePoint + ccp(0, - 320));
	sprite4->setOpacity(100);
	sprite4->setColor(ccc3(64, 198, 255));
	sprite4->setBlendFunc(f);
	sprite4->runAction(CCRepeatForever::create(CCSequence::create(CCScaleTo::create(timeScale, sprite4->getScale() + scaleDelta1), CCScaleTo::create(timeScale, sprite4->getScale() - scaleDelta1), NULL)));
	plane->addChild(sprite4, 1, 23);

	schedule(SEL_SCHEDULE(&SpeedRunBuff::processing));

	getPlane()->getMainLayer()->getMap()->setSpeedUpFactor(SPEEDUPBUFF_FACTOR);
	dynamic_cast<GameBackground*>(getPlane()->getMainLayer()->getParent()->getChildByTag(BACKGROUNDLAYER_TAG))->setSpeedUpFactor(SPEEDUPBUFF_FACTOR);
	getPlane()->getMainLayer()->getInputLayer()->getSpeedrunBar()->setVisible(true);

	// Set new bound
	setProtectBoxBK(getPlane()->getProtectBoundBox());
	getPlane()->setProtectBoundBox(PROTECT_BOX);
}

void SpeedRunBuff::onComplete()
{
	PlayerPlane* plane = getPlane();
	plane->setIsProtected(false);

	CCObject* it;
	CCARRAY_FOREACH(_listWave, it)
	{
		plane->removeChild(dynamic_cast<CCSprite*>(it));
	}
	plane->removeChildByTag(20);
	plane->removeChildByTag(21);
	plane->removeChildByTag(22);
	plane->removeChildByTag(23);
	unschedule(SEL_SCHEDULE(&SpeedRunBuff::processing));

	getPlane()->getMainLayer()->getMap()->setSpeedUpFactor(1.0f);
	dynamic_cast<GameBackground*>(getPlane()->getMainLayer()->getParent()->getChildByTag(BACKGROUNDLAYER_TAG))->setSpeedUpFactor(1.0f);
	getPlane()->getMainLayer()->getInputLayer()->getSpeedrunBar()->setVisible(false);

	// Kill all enemy
	CCArray* listEnemy = plane->getMainLayer()->getEnemies();

	CCARRAY_FOREACH(listEnemy, it)
	{
		dynamic_cast<Enemy*>(it)->setHealth(-1);
	}

	getPlane()->setProtectBoundBox(CCSizeZero);
}

void SpeedRunBuff::processing( float dt )
{
	float timeDelay = 0.1f;
	float maxWave = 4;
	float beginScale = 1.0f;
	float velY = -1200;
	float scaleFactor = 3.0f;
	CCPoint planePoint = ccp(getPlane()->boundingBox().size.width/2, getPlane()->boundingBox().size.height/2);

	if (_currentTime > timeDelay)
	{
		if (_listWave->count() < maxWave)
		{
			CCSprite* wave = CCSprite::create("superglow05.png");
			wave->setPosition(planePoint);
			wave->setOpacity(255);
			wave->setScale(beginScale);
			getPlane()->addChild(wave);
			_listWave->addObject(wave);
		} else
		{
			CCSprite* firstWave = dynamic_cast<CCSprite*>(_listWave->objectAtIndex(0));
			firstWave->setScale(beginScale);
			firstWave->setOpacity(255);
			_listWave->removeObjectAtIndex(0);
			_listWave->addObject(firstWave);
			firstWave->setPosition(planePoint);
		}
		_currentTime = 0;
	}

	for (int i = 0; i < _listWave->count(); i++)
	{
		CCSprite* wave = dynamic_cast<CCSprite*>(_listWave->objectAtIndex(i));
		wave->setPositionY(wave->getPositionY() + velY * dt);
		wave->setScale(wave->getScale() + dt * scaleFactor);
		float a = (i + 1) * (255/4) - (_currentTime / timeDelay) * (255/4);
		wave->setOpacity(a);
	}

	_currentTime += dt;

	// When end map speed up must be off
	bool isBossNow = getPlane()->getMainLayer()->getMap()->getIsBossNow();
	if (isBossNow)
	{
		stop();
	}

	// Update process bar on MainLayer
	getPlane()->getMainLayer()->getInputLayer()->getSpeedrunBar()->setPercent((1 - getTime()/getTimeMax()) * 100);
}
